package cc.ufinity.networkdiagnose.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import cc.ufinity.networkdiagnose.app.App;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.test.Location;
import cc.ufinity.networkdiagnose.widget.HttpConfigWidget;
import cc.ufinity.networkdiagnose.widget.PingConfigWidget;
import cc.ufinity.networkdiagnose.widget.TaskWidget;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

public class HomePageActivity extends Activity{
	private LinearLayout TaskLayout;
	private LinearLayout EvrLayout;
	private LinearLayout SystemConfigLayout;
	private LinearLayout TeskLayout;
	
	private int gpsOrNet = LocationClientOption.GpsFirst;
	public static LocationClient mLocClient;
	private Handler handler;
	
	/**
	 * 四个按钮，检查更新，关于，问题反馈，帮助*/
	private Button update ,about,feedback,help;
	 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.firstpage);
		TaskLayout=(LinearLayout)findViewById(R.id.taskTest);
		EvrLayout=(LinearLayout)findViewById(R.id.Environmentalinformation);
		SystemConfigLayout=(LinearLayout)findViewById(R.id.Systemconfig);
		TeskLayout=(LinearLayout)findViewById(R.id.randomTest);
		update=(Button)findViewById(R.id.update);
		help=(Button)findViewById(R.id.help);
		about=(Button)findViewById(R.id.about);
		feedback=(Button)findViewById(R.id.feedback);
		
		
		
		handler=new Handler(){

			@Override
			public void handleMessage(Message msg) {
				switch (msg.what){
				case 1:
					try{
						if(mLocClient.isStarted()){
							mLocClient.stop();
						} 
					}catch(Exception e){ }
					break;
				}
			}
			
		};
		
		Location loc=new Location(getApplicationContext(),handler);
		mLocClient=loc.mLocationClient;
		setLocationOption();
		mLocClient.start();
		
		
		SharedPreferences settings1 = this.getSharedPreferences(GlobalApp.CFG_SERVER, 0);
        String ip = settings1.getString(GlobalApp.CFG_SERVER_IP, "");
        GlobalApp.FTP = ip;
		
		
		
		 //初始化ping的次数为1；默认http网址为www.baidu.com
		SharedPreferences settings = this.getSharedPreferences(PingConfigWidget.PREFS_PING_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        String Number = settings.getString(PingConfigWidget.PREFS_PING_KEY, "");
        if(Number.equals("") || Number.equals(null)){
	        editor.putString(PingConfigWidget.PREFS_PING_KEY, "1");
	        editor.commit();
        }
        
        
        settings = this.getSharedPreferences(HttpConfigWidget.PREFS_HTTP_NAME, 0);
        editor = settings.edit();
        String url = settings.getString(HttpConfigWidget.PREFS_HTTP_KEY, "");
        if(url.equals("") || url.equals(null)){
        	editor.putString(HttpConfigWidget.PREFS_HTTP_KEY, "www.baidu.com");
            editor.commit();	
        }
        
        //开启taskService
		 //打开或创建preTasks.db数据库  
        SQLiteDatabase db = openOrCreateDatabase("preTasks.db", Context.MODE_PRIVATE, null);  
      //创建person表  
        try {
            db.execSQL("create table tasks (_id integer primary key autoincrement, "
            		+ "taskid varchar(100) not null)"); 
		} catch (Exception e) {}
		db.close();
        // startService(new Intent(this, TaskService.class));	
		
		
		TaskLayout.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent intent=new Intent();
				TaskWidget.index=0;
				intent.setClass(HomePageActivity.this, TaskActivity.class);
				startActivity(intent);
			}
		});
		
		EvrLayout.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setClass(HomePageActivity.this, EnvironmentActivity.class);
				startActivity(intent);
			}
		});
		
		SystemConfigLayout.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setClass(HomePageActivity.this, CfgActivity.class);
				startActivity(intent);
			}
		});
		
		TeskLayout.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setClass(HomePageActivity.this, TestActivity.class);
				startActivity(intent);
			}
		});
		
		update.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Toast.makeText(HomePageActivity.this, "当前没有新的版本", Toast.LENGTH_SHORT).show();
			}
		});
		
		feedback.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setClass(HomePageActivity.this, QuestionBackActivity.class);
				startActivity(intent);
			}
		});
		
		about.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setClass(HomePageActivity.this, AboutActivity.class);
				startActivity(intent);
			}
		});
		
		help.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setClass(HomePageActivity.this, NetWorkHelp.class);
				startActivity(intent);
			}
		});
		
		new App(this);
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("提示");
			builder.setMessage("确定退出系统吗？");
			builder.setCancelable(false);
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
							android.os.Process.killProcess(android.os.Process.myPid());
						}
					});
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
			builder.show();
		}
		return false;
	}
	
	
	
	/**
	 * 获取的是经纬度所需要的参数的设置
	 */
	private void setLocationOption(){
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);			 
		option.setCoorType("bd09ll");		 
		option.setServiceName("com.baidu.location.service_v2.9");
		option.setScanSpan(1000); 
		option.setAddrType("all");  
		option.setPriority(gpsOrNet);        
//		option.setPoiNumber(10);
		option.disableCache(true);		
		mLocClient.setLocOption(option);
	}
}
